﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace yq.Log
{
    /// <summary>
    /// 相关配置
    /// </summary>
    internal class Config
    {

        internal static object LockObj = new object();

        internal static string DbProviderName = "";

        internal static string DbConnectionString = "";

        internal static bool IsEnable = false;



        /// <summary>
        /// 配置文件路径
        /// </summary>
        private const string _DefaultFilePath = "Log.config";

        /// <summary>
        /// 站点配置文件路径
        /// </summary>
        private static string _ConfigFilePath = string.Empty;
        /// <summary>
        /// 站点配置文件路径
        /// </summary>
        private static string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_ConfigFilePath))
                {
                    _ConfigFilePath = cn.Util.WebConfig.ConfigFilePath + _DefaultFilePath;
                }

                return _ConfigFilePath;
            }
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        static Config()
        {
            if (System.IO.File.Exists(ConfigFilePath))
            {
                var root = XElement.Load(ConfigFilePath, LoadOptions.None);
                var allElement = from obj in root.Elements()
                                 select obj;

                foreach (XElement host in allElement)
                {
                    if (host.Name.LocalName == "connection")
                    {
                        DbConnectionString = GetXElementValue(host);
                    }
                    else if (host.Name.LocalName == "providerName")
                    {
                        DbProviderName = GetXElementValue(host);
                    }
                }

                if (allElement.Count() != 0)
                {
                    IsEnable = true;
                }
            }
        }

        /// <summary>
        /// 取value
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        private static string GetXElementValue(XElement host)
        {
            return host.Attribute(XName.Get("value")) == null ? "" : host.Attribute(XName.Get("value")).Value;
        }
    }
}
